<!--编辑设备-->
<template>
  <div class="main">
    <div class="info-container">
      <div class="info-container_body">
        <el-form ref="refFrom" label-position="top" class="demo" v-loading="loading">
          <el-form-item label="关联站房">
            <el-select v-model="item.stationId" placeholder="关联站房">
              <el-option v-for="item in dict.stations" :key="item.stationId" :label="item.name" :value="item.stationId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="item.status" placeholder="状态">
              <el-option v-for="item in dict.device_status" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" rows="3" v-model="item.remarks" />
          </el-form-item>
        </el-form>
      </div>
      <div class="info-container_footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSave">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { listStation } from '@/api/station/index';
import { getDevice, updateDevice } from '@/api/device/index';
import common from '@/utils/common.ts';

export default {
  mounted() {
    const { proxy } = getCurrentInstance() as ComponentInternalInstance;
    this.proxy = proxy;
    this.dict = toRefs<any>(this.proxy?.useDict('device_status'));

    this.init();
  },
  props: {
    data: {}
  },
  computed: {},
  data() {
    return {
      proxy: null,
      dict: {},
      loading: false,
      item: {}
    };
  },
  methods: {
    init() {
      this.item = JSON.parse(JSON.stringify(this.data));
      this.queryStations();
    },

    queryStations() {
      const queryParams = ref<PageQuery>({
        pageNum: 1,
        pageSize: 2000
      });

      listStation(queryParams.value).then((res) => {
        this.dict.stations = res.rows;
      });
    },

    handleCancel() {
      this.$emit('hideRight');
    },

    handleSave() {
      this.loading = true;
      updateDevice(this.item)
        .then((res) => {
          this.loading = false;
          if (res.code == 200) {
            this.$emit('hideRight');
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {
          this.loading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.main {
  height: 100%;

  .contentFrm {
    padding: 10px;
    background: #f6f7fb;
  }

  .el-input-number__decrease,
  .el-input-number__increase {
    display: none;
  }

  .el-input-number {
    width: 100%;
  }
}
</style>